<?php
require_once 'modules/dao/constants.php';
require_once 'modules/dao/connection.php';

class BaseSetUp {

    public static function setUp () {
        self::createDataBase();
        self::createTables();
    }

    private static function createTables () {
        foreach (glob(self::getFiles()) as $file) {
            $sql = file_get_contents($file);
            Connection::getInstance()->getConnection()->query($sql);
            Connection::getInstance()->closeConnection(); # 如果不清除，其它表将无法创建
        }
        Connection::getInstance()->closeConnection();
    }

    private static function getFiles () {
        return dirname(__FILE__) . '/../../doc/database/*.sql';
    }

    private static function createDataBase () {
        $config = self::setTest();
        $conn = mysql_connect($config[Consts::F_DBHOST], $config[Consts::F_DBUSER], $config[Consts::F_DBPASS]);
        mysql_select_db('mysql', $conn);
        mysql_query("DROP DATABASE IF EXISTS " . $config[Consts::F_DBNAME]);
        mysql_query("CREATE DATABASE IF NOT EXISTS " . $config[Consts::F_DBNAME]);
        mysql_close();
        return;
    }

    private static function setTest () {
        $database = array();
        $database[Consts::F_DBHOST] = 'localhost';
        $database[Consts::F_DBUSER] = 'root';
        $database[Consts::F_DBPASS] = '';
        $database[Consts::F_DBNAME] = 'igrid_new';
        return $database;
    }
}
?>